import json

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import Setting, DeskTop


class PreTest_RK3568_Basic_Setting_3300(TestCase, Setting):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1.点击设置应用
    # 2.点击隐私
    # 3.点击权限管理，点击权限
    # 4.反复上下滑动权限列表，左右滑动或点击应用\权限切换
    # 5.点击进入权限列表中某一内容列，应用权限搜索、按钮开启关闭
    # 预期结果：
    # 1.进入设置应用
    # 2.进入隐私
    # 3.进入权限管理-权限
    # 4.可以上下左右滑动或点击权限/应用，无卡死、黑屏、白屏现象
    # 5.应用权限搜索、按钮开启关闭生效无异常

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：点击设置应用')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        Step('预期结果1：进入设置应用')
        CommonOH.checkIfTextExist(self.Phone1, '设置')
        Step('步骤2：点击隐私')
        CommonOH.touchByText(self.Phone1, '隐私')
        CommonOH.wait(self.Phone1, 0.5)
        Step('预期结果2：进入隐私')
        CommonOH.checkIfTextExist(self.Phone1, '权限管理')
        Step('步骤3：点击点击权限管理，点击权限')
        CommonOH.touchByText(self.Phone1, '权限管理')
        CommonOH.wait(self.Phone1, 0.5)
        CommonOH.touchByText(self.Phone1, '权限')
        Step('预期结果3：进入权限管理-权限')
        CommonOH.checkIfTextExist(self.Phone1, '位置信息')
        Step('步骤4：反复上下滑动权限列表，左右滑动或点击应用、权限切换')
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_left, steps=300)
        Step('预期结果4：可以上下左右滑动或点击权限/应用，无卡死、黑屏、白屏现象')
        CommonOH.wait(self.Phone1, 1)
        CommonOH.checkIfTypeExist(self.Phone1, 'TextInput')
        Step('步骤5：点击进入权限列表中某一内容列，应用权限搜索、按钮开启关闭')
        CommonOH.touchByType(self.Phone1, 'TextInput')
        CommonOH.inputTextByType(self.Phone1, type='TextInput', content='D')
        Step('预期结果5.应用权限搜索、按钮开启关闭生效无异常')
        CommonOH.checkIfTextExist(self.Phone1, 'DistributedMusicPlayer')
        CommonOH.touchByText(self.Phone1, '权限')
        CommonOH.touchByText(self.Phone1, '多设备协同')

        property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
        checked0 = property_dict.get('checked')

        CommonOH.touchByType(self.Phone1, 'Toggle')
        property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
        checked1 = property_dict.get('checked')
        Asserts.assert_not_equal(checked0, checked1)

        CommonOH.touchByType(self.Phone1, 'Toggle')
        property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
        checked2 = property_dict.get('checked')
        Asserts.assert_equal(checked0, checked2)

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
